package org.millioncall.yueyoga.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.service.UserService;
import org.millioncall.yueyoga.common.GlobalConstants;
import org.millioncall.yueyoga.common.GlobalSiteConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//created by zangjy on 2012-4-11
public class FrontUtil {
	private static Logger logger = LoggerFactory.getLogger(FrontUtil.class);

	public static Map<String, Object> createBaseArrtibutes(
			HttpServletRequest request) {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("base", path);// 项目根路径
		map.put("location", basePath);//
		map.put("resPath", path + "/resources");
		map.put("commonResPath", path + "/common/resources");
		map.put("sitename", GlobalConstants.APP_WEB_NAME);
		map.put("poweredby", GlobalConstants.APP_POWEREDBY);
		map.put("serverDate", new Date());
		return map;
	}

	public static Map<String, Object> createSessionUserAttributes(
			HttpServletRequest request) throws NoSessionUserException {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) request.getSession().getAttribute(
				GlobalConstants.LOGIN_USER_KEY);
		if (user == null) {
			throw new NoSessionUserException();
		}

		UserService us = (UserService) WebAppUtils.getSpringBean(request,
				"userService");
		map.put("user", user);
		map.put("usergroups", us.getCnStrGroups(user.getGroups()));
		map.put("userinterest", user.getUserDetail().getStrInterest());

		if (user.getHeadImgFile() != null) {
			String headpicpath = createDbImageUrl(request,
					user.getHeadImgFileName());
			if (headpicpath != null) {
				map.put("userheadpicpath", headpicpath);
			}
		} else {
			map.put("userheadpicpath", FrontUtil.getDefaultHeadPicUrl(request));
		}
		return map;
	}

	public static class NoSessionUserException extends Exception {
		private static final long serialVersionUID = -2211240530437014309L;

		public NoSessionUserException() {
			super("session中无注册的用户");
		}
	}

	// 根据数据库文件名，解析为图像的URL请求地址
	public static String createDbImageUrl(HttpServletRequest request,
			String dbImageFileName) {
		if (dbImageFileName == null) {
			return null;
		}
		try {
			return request.getServletContext().getContextPath() + ""
					+ URLEncoder.encode(dbImageFileName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("URL加密时编码异常");
			return null;
		}
	}

	// 取得默认封面图
	public static String getDefaultCoverImageUrl(HttpServletRequest request) {
		return request.getServletContext().getContextPath() + "/"
				+ GlobalSiteConfig.getDefaultCoverImagePath();
	}

	// 取得默认头像
	public static String getDefaultHeadPicUrl(HttpServletRequest request) {
		int k = new Double(Math.floor(Math.random() * 7)).intValue();
		return request.getServletContext().getContextPath() + "/"
				+ GlobalSiteConfig.getDefaultImagePath() + "default_avatar_"
				+ k + ".png";
	}

	public static String getBrowserType(String agent) {
		if (agent.indexOf("msie 7") > 0) {
			return "ie7";
		} else if (agent.indexOf("msie 8") > 0) {
			return "ie8";
		} else if (agent.indexOf("msie 9") > 0) {
			return "ie9";
		} else if (agent.indexOf("msie 10") > 0) {
			return "ie10";
		} else if (agent.indexOf("msie") > 0) {
			return "ie";
		} else if (agent.indexOf("opera") > 0) {
			return "opera";
		} else if (agent.indexOf("opera") > 0) {
			return "opera";
		} else if (agent.indexOf("firefox") > 0) {
			return "firefox";
		} else if (agent.indexOf("webkit") > 0) {
			return "webkit";
		} else if (agent.indexOf("gecko") > 0 && agent.indexOf("rv:11") > 0) {
			return "ie11";
		} else {
			return "Others";
		}
	}

}
